
#include "../ui.h"
#include "../app_includes.h"

static lv_obj_t *ui_screen;
static lv_obj_t *ui_Panel7;
static lv_obj_t *ui_LabelValue;
static lv_obj_t *ui_LabelUnit;
static lv_obj_t *ui_LabelAuto;
static lv_obj_t *ui_LabelHold;
static lv_obj_t *ui_Panel8;
static lv_obj_t *ui_BtnSelect[4];
static lv_obj_t *ui_LabelSelect[4];
static lv_obj_t *ui_Panel9;
static lv_obj_t *ui_BtnMode;
static lv_obj_t *ui_Label22;
static lv_obj_t *ui_BtnHold;
static lv_obj_t *ui_Label27;
static lv_obj_t *ui_BtnGear;
static lv_obj_t *ui_Label28;
static lv_obj_t *ui_LabelRModeFlag;

static lv_obj_t *ui_PanelFlag;
static lv_obj_t *ui_PanelCom1;
static lv_obj_t *ui_PanelCom1_1;
static lv_obj_t *ui_PanelCom2;
static lv_obj_t *ui_PanelCom2_1;
static lv_obj_t *ui_PanelCom3;
static lv_obj_t *ui_PanelCom3_1;
static lv_obj_t *ui_PanelCom3_2;
static lv_obj_t *ui_PanelCom4;
static lv_obj_t *ui_PanelCom4_1;
static lv_obj_t *ui_PanelCom4_2;

static lv_timer_t *lv_timer;

static char *r_flag[3] = {"", "D", "B"};
static uint8_t channel[4] = {kBoardMetetCh_V,kBoardMetetCh_mV,kBoardMetetCh_A,kBoardMetetCh_R,};

static void meter_value_refresh(lv_timer_t *timer);
static void sync_ui(multimeter_t *m);
static void update_ui(multimeter_t *m);
static void btn_select_event_cb(lv_event_t * e);
static void btn_gear_event_cb(lv_event_t * e);
static void btn_mode_event_cb(lv_event_t * e);

void lv_mutimeter_close(void)
{  
    
    multimeter_select_channel(channel[0]);
    lv_timer_del(lv_timer);
}

//app应用入口
lv_obj_t *lv_mutimeter_open(void)
{
    ui_screen = lv_screen_create(ui_main_screen.desktop, LV_HOR_RES, LV_VER_RES);
    
    ui_Panel7 = lv_obj_create(ui_screen);
    lv_obj_set_width( ui_Panel7, 400);
    lv_obj_set_height( ui_Panel7, 100);
    lv_obj_align( ui_Panel7, LV_ALIGN_TOP_MID, 0, 50);
    lv_obj_clear_flag( ui_Panel7, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_Panel7, 30, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelValue = lv_label_create(ui_Panel7);
    lv_obj_set_width( ui_LabelValue, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelValue, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_LabelValue, LV_ALIGN_LEFT_MID );
    lv_label_set_text(ui_LabelValue,"-L.LLLL");
    lv_obj_set_style_text_font(ui_LabelValue, &ui_font_FontNumber64, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelUnit = lv_label_create(ui_Panel7);
    lv_obj_set_width( ui_LabelUnit, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelUnit, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelUnit, 65 );
    lv_obj_set_y( ui_LabelUnit, 10 );
    lv_obj_set_align( ui_LabelUnit, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelUnit,"mV");
    lv_obj_set_style_text_font(ui_LabelUnit, &ui_font_FontChinese, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    ui_LabelRModeFlag = lv_label_create(ui_Panel7);
    lv_obj_set_width( ui_LabelRModeFlag, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelRModeFlag, LV_SIZE_CONTENT);   /// 1
    lv_obj_align_to( ui_LabelRModeFlag, ui_LabelUnit, LV_ALIGN_OUT_TOP_LEFT, 0, -15 );
    lv_label_set_text(ui_LabelRModeFlag,"");
    lv_obj_set_style_text_font(ui_LabelRModeFlag, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    

    ui_LabelAuto = lv_label_create(ui_Panel7);
    lv_obj_set_width( ui_LabelAuto, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelAuto, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelAuto, 145 );
    lv_obj_set_y( ui_LabelAuto, -20 );
    lv_obj_set_align( ui_LabelAuto, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelAuto," auto ");
    lv_obj_set_style_text_font(ui_LabelAuto, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelAuto, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_LabelAuto, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_LabelAuto, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_LabelAuto, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    

    ui_LabelHold = lv_label_create(ui_Panel7);
    lv_obj_set_width( ui_LabelHold, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelHold, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_x( ui_LabelHold, 146 );
    lv_obj_set_y( ui_LabelHold, 20 );
    lv_obj_set_align( ui_LabelHold, LV_ALIGN_CENTER );
    lv_label_set_text(ui_LabelHold," hold ");
    lv_obj_set_style_text_font(ui_LabelHold, &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_LabelHold, 5, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_LabelHold, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_LabelHold, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_LabelHold, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_Panel8 = lv_obj_create(ui_screen);
    lv_obj_set_width( ui_Panel8, 200);
    lv_obj_set_height( ui_Panel8, 200);
    lv_obj_set_x( ui_Panel8, -90 );
    lv_obj_set_y( ui_Panel8, 60 );
    lv_obj_set_align( ui_Panel8, LV_ALIGN_CENTER );
    lv_obj_clear_flag( ui_Panel8, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_Panel8, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_Panel8, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_Panel8, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_color(ui_Panel8, lv_color_hex(0x00B4B4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_border_opa(ui_Panel8, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_Panel8, 5, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_BtnSelect[0] = lv_btn_create(ui_screen);
    lv_obj_set_width( ui_BtnSelect[0], 50);
    lv_obj_set_height( ui_BtnSelect[0], 100);
    lv_obj_align_to( ui_BtnSelect[0], ui_Panel8, LV_ALIGN_LEFT_MID, -5, 0 );
    lv_obj_add_flag( ui_BtnSelect[0], LV_OBJ_FLAG_CHECKABLE | LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( ui_BtnSelect[0], LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_BtnSelect[0], 200, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_BtnSelect[0], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BtnSelect[0], 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_BtnSelect[0], 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_BtnSelect[0], 200, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_bg_color(ui_BtnSelect[0], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_CHECKED );
    lv_obj_set_style_bg_opa(ui_BtnSelect[0], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_border_width(ui_BtnSelect[0], 0, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_color(ui_BtnSelect[0], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_CHECKED );
    lv_obj_set_style_shadow_opa(ui_BtnSelect[0], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_width(ui_BtnSelect[0], 40, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_spread(ui_BtnSelect[0], 15, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_opa(ui_BtnSelect[0], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_add_event_cb(ui_BtnSelect[0], btn_select_event_cb, LV_EVENT_CLICKED, NULL);

    ui_BtnSelect[1] = lv_btn_create(ui_screen);
    lv_obj_set_width( ui_BtnSelect[1], 100);
    lv_obj_set_height( ui_BtnSelect[1], 50);
    lv_obj_align_to( ui_BtnSelect[1], ui_Panel8, LV_ALIGN_TOP_MID, 0, -5 );
    lv_obj_add_flag( ui_BtnSelect[1], LV_OBJ_FLAG_CHECKABLE | LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( ui_BtnSelect[1], LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_BtnSelect[1], 200, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_BtnSelect[1], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BtnSelect[1], 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_BtnSelect[1], 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_BtnSelect[1], 200, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_bg_color(ui_BtnSelect[1], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_CHECKED);
    lv_obj_set_style_bg_opa(ui_BtnSelect[1], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_border_width(ui_BtnSelect[1], 0, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_color(ui_BtnSelect[1], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_CHECKED );
    lv_obj_set_style_shadow_opa(ui_BtnSelect[1], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_width(ui_BtnSelect[1], 40, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_spread(ui_BtnSelect[1], 15, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_opa(ui_BtnSelect[1], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_add_event_cb(ui_BtnSelect[1], btn_select_event_cb, LV_EVENT_CLICKED, NULL);

    ui_BtnSelect[2] = lv_btn_create(ui_screen);
    lv_obj_set_width( ui_BtnSelect[2], 100);
    lv_obj_set_height( ui_BtnSelect[2], 50);
    lv_obj_align_to( ui_BtnSelect[2], ui_Panel8, LV_ALIGN_BOTTOM_MID, 0, 8 );
    lv_obj_add_flag( ui_BtnSelect[2], LV_OBJ_FLAG_CHECKABLE | LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( ui_BtnSelect[2], LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_BtnSelect[2], 200, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_BtnSelect[2], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BtnSelect[2], 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_BtnSelect[2], 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_BtnSelect[2], 200, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_bg_color(ui_BtnSelect[2], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_CHECKED );
    lv_obj_set_style_bg_opa(ui_BtnSelect[2], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_border_width(ui_BtnSelect[2], 0, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_color(ui_BtnSelect[2], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_CHECKED );
    lv_obj_set_style_shadow_opa(ui_BtnSelect[2], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_width(ui_BtnSelect[2], 40, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_spread(ui_BtnSelect[2], 15, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_opa(ui_BtnSelect[2], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_add_event_cb(ui_BtnSelect[2], btn_select_event_cb, LV_EVENT_CLICKED, NULL);

    ui_BtnSelect[3] = lv_btn_create(ui_screen);
    lv_obj_set_width( ui_BtnSelect[3], 50);
    lv_obj_set_height( ui_BtnSelect[3], 100);
    lv_obj_align_to( ui_BtnSelect[3], ui_Panel8, LV_ALIGN_RIGHT_MID, 8, 0 );
    lv_obj_add_flag( ui_BtnSelect[3], LV_OBJ_FLAG_CHECKABLE | LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( ui_BtnSelect[3], LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_BtnSelect[3], 200, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_BtnSelect[3], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BtnSelect[3], 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_opa(ui_BtnSelect[3], 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_BtnSelect[3], 200, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_bg_color(ui_BtnSelect[3], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_CHECKED );
    lv_obj_set_style_bg_opa(ui_BtnSelect[3], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_border_width(ui_BtnSelect[3], 0, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_color(ui_BtnSelect[3], lv_color_hex(0xABC0D4), LV_PART_MAIN | LV_STATE_CHECKED );
    lv_obj_set_style_shadow_opa(ui_BtnSelect[3], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_width(ui_BtnSelect[3], 40, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_shadow_spread(ui_BtnSelect[3], 15, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_set_style_opa(ui_BtnSelect[3], 255, LV_PART_MAIN| LV_STATE_CHECKED);
    lv_obj_add_event_cb(ui_BtnSelect[3], btn_select_event_cb, LV_EVENT_CLICKED, NULL);

    ui_Panel9 = lv_obj_create(ui_screen);
    lv_obj_set_width( ui_Panel9, 184);
    lv_obj_set_height( ui_Panel9, 184);
    lv_obj_set_x( ui_Panel9, -90 );
    lv_obj_set_y( ui_Panel9, 60 );
    lv_obj_set_align( ui_Panel9, LV_ALIGN_CENTER );
    lv_obj_add_flag( ui_Panel9, LV_OBJ_FLAG_ADV_HITTEST );   /// Flags
    lv_obj_clear_flag( ui_Panel9, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_Panel9, 100, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelSelect[0] = lv_label_create(ui_Panel9);
    lv_obj_set_width( ui_LabelSelect[0], LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelSelect[0], LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_LabelSelect[0], LV_ALIGN_LEFT_MID );
    lv_label_set_text(ui_LabelSelect[0],"V");
    lv_obj_set_style_text_color(ui_LabelSelect[0], lv_color_hex(0x808080), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelSelect[0], 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelSelect[0], &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    ui_LabelSelect[1] = lv_label_create(ui_Panel9);
    lv_obj_set_width( ui_LabelSelect[1], LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelSelect[1], LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_LabelSelect[1], LV_ALIGN_TOP_MID );
    lv_label_set_text(ui_LabelSelect[1],"mV");
    lv_obj_set_style_text_color(ui_LabelSelect[1], lv_color_hex(0x808080), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelSelect[1], 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelSelect[1], &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelSelect[2] = lv_label_create(ui_Panel9);
    lv_obj_set_width( ui_LabelSelect[2], LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelSelect[2], LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_LabelSelect[2], LV_ALIGN_BOTTOM_MID );
    lv_label_set_text(ui_LabelSelect[2],"A");
    lv_obj_set_style_text_color(ui_LabelSelect[2], lv_color_hex(0x808080), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelSelect[2], 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelSelect[2], &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_LabelSelect[3] = lv_label_create(ui_Panel9);
    lv_obj_set_width( ui_LabelSelect[3], LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_LabelSelect[3], LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_LabelSelect[3], LV_ALIGN_RIGHT_MID );
    lv_label_set_text(ui_LabelSelect[3],"R");
    lv_obj_set_style_text_color(ui_LabelSelect[3], lv_color_hex(0x808080), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_text_opa(ui_LabelSelect[3], 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_LabelSelect[3], &lv_font_montserrat_22, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_BtnMode = lv_btn_create(ui_screen);
    lv_obj_set_width( ui_BtnMode, 100);
    lv_obj_set_height( ui_BtnMode, 50);
    lv_obj_set_x( ui_BtnMode, 130 );
    lv_obj_set_y( ui_BtnMode, 60 );
    lv_obj_set_align( ui_BtnMode, LV_ALIGN_CENTER );
    lv_obj_add_flag( ui_BtnMode, LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( ui_BtnMode, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_BtnMode, lv_color_hex(0x798028), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BtnMode, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_color(ui_BtnMode, lv_color_hex(0x00B4B4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_border_opa(ui_BtnMode, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_BtnMode, 2, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_add_event_cb(ui_BtnMode, btn_mode_event_cb, LV_EVENT_CLICKED, NULL);

    ui_Label22 = lv_label_create(ui_BtnMode);
    lv_obj_set_width( ui_Label22, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_Label22, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_Label22, LV_ALIGN_CENTER );
    lv_label_set_text(ui_Label22,"MODE");

    ui_BtnHold = lv_btn_create(ui_screen);
    lv_obj_set_width( ui_BtnHold, 100);
    lv_obj_set_height( ui_BtnHold, 50);
    lv_obj_set_x( ui_BtnHold, 130 );
    lv_obj_set_y( ui_BtnHold, -10 );
    lv_obj_set_align( ui_BtnHold, LV_ALIGN_CENTER );
    lv_obj_add_flag( ui_BtnHold, LV_OBJ_FLAG_CHECKABLE | LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( ui_BtnHold, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_BtnHold, lv_color_hex(0x6D60D3), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BtnHold, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_color(ui_BtnHold, lv_color_hex(0x00B4B4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_border_opa(ui_BtnHold, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_BtnHold, 2, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_Label27 = lv_label_create(ui_BtnHold);
    lv_obj_set_width( ui_Label27, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_Label27, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_Label27, LV_ALIGN_CENTER );
    lv_label_set_text(ui_Label27,"HOLD");

    ui_BtnGear = lv_btn_create(ui_screen);
    lv_obj_set_width( ui_BtnGear, 100);
    lv_obj_set_height( ui_BtnGear, 50);
    lv_obj_set_x( ui_BtnGear, 130 );
    lv_obj_set_y( ui_BtnGear, 130 );
    lv_obj_set_align( ui_BtnGear, LV_ALIGN_CENTER );
    lv_obj_add_flag( ui_BtnGear, LV_OBJ_FLAG_SCROLL_ON_FOCUS );   /// Flags
    lv_obj_clear_flag( ui_BtnGear, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_BtnGear, lv_color_hex(0x201F26), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_BtnGear, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_color(ui_BtnGear, lv_color_hex(0x00B4B4), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_border_opa(ui_BtnGear, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_BtnGear, 2, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_add_event_cb(ui_BtnGear, btn_gear_event_cb, LV_EVENT_CLICKED, NULL);

    ui_Label28 = lv_label_create(ui_BtnGear);
    lv_obj_set_width( ui_Label28, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height( ui_Label28, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_align( ui_Label28, LV_ALIGN_CENTER );
    lv_label_set_text(ui_Label28,"RANGE");


    ui_PanelFlag = lv_obj_create(ui_screen);
    lv_obj_set_width( ui_PanelFlag, 400);
    lv_obj_set_height( ui_PanelFlag, 50);
    lv_obj_set_x( ui_PanelFlag, 0 );
    lv_obj_set_y( ui_PanelFlag, 0 );
    lv_obj_set_align( ui_PanelFlag, LV_ALIGN_BOTTOM_MID );
    lv_obj_clear_flag( ui_PanelFlag, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelFlag, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelFlag, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui_PanelFlag, 0, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCom2 = lv_obj_create(ui_PanelFlag);
    lv_obj_set_width( ui_PanelCom2, 30);
    lv_obj_set_height( ui_PanelCom2, 30);
    lv_obj_set_x( ui_PanelCom2, -100 );
    lv_obj_set_y( ui_PanelCom2, 0 );
    lv_obj_set_align( ui_PanelCom2, LV_ALIGN_CENTER );
    lv_obj_clear_flag( ui_PanelCom2, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelCom2, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelCom2, lv_color_hex(0x454444), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCom2, 50, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCom2_1 = lv_obj_create(ui_PanelCom2);
    lv_obj_set_width( ui_PanelCom2_1, 15);
    lv_obj_set_height( ui_PanelCom2_1, 15);
    lv_obj_set_align( ui_PanelCom2_1, LV_ALIGN_CENTER );
    lv_obj_clear_flag( ui_PanelCom2_1, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelCom2_1, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelCom2_1, lv_color_hex(0xC6F531), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCom2_1, 50, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCom1 = lv_obj_create(ui_PanelFlag);
    lv_obj_set_width( ui_PanelCom1, 30);
    lv_obj_set_height( ui_PanelCom1, 30);
    lv_obj_set_x( ui_PanelCom1, -180 );
    lv_obj_set_y( ui_PanelCom1, 0 );
    lv_obj_set_align( ui_PanelCom1, LV_ALIGN_CENTER );
    lv_obj_clear_flag( ui_PanelCom1, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelCom1, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelCom1, lv_color_hex(0x454444), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCom1, 50, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCom1_1 = lv_obj_create(ui_PanelCom1);
    lv_obj_set_width( ui_PanelCom1_1, 15);
    lv_obj_set_height( ui_PanelCom1_1, 15);
    lv_obj_set_align( ui_PanelCom1_1, LV_ALIGN_CENTER );
    lv_obj_clear_flag( ui_PanelCom1_1, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelCom1_1, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelCom1_1, lv_color_hex(0xC6F531), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCom1_1, 50, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCom3 = lv_obj_create(ui_PanelFlag);
    lv_obj_set_width( ui_PanelCom3, 60);
    lv_obj_set_height( ui_PanelCom3, 30);
    lv_obj_set_x( ui_PanelCom3, 50 );
    lv_obj_set_y( ui_PanelCom3, 0 );
    lv_obj_set_align( ui_PanelCom3, LV_ALIGN_CENTER );
    lv_obj_clear_flag( ui_PanelCom3, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelCom3, lv_color_hex(0x3D3B48), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCom3, 255, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCom3_1 = lv_obj_create(ui_PanelCom3);
    lv_obj_set_width( ui_PanelCom3_1, 20);
    lv_obj_set_height( ui_PanelCom3_1, 20);
    lv_obj_align( ui_PanelCom3_1, LV_ALIGN_LEFT_MID, -5, 0);
    lv_obj_clear_flag( ui_PanelCom3_1, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelCom3_1, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelCom3_1, lv_color_hex(0xC6F531), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCom3_1, 255, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCom3_2 = lv_obj_create(ui_PanelCom3);
    lv_obj_set_width( ui_PanelCom3_2, 20);
    lv_obj_set_height( ui_PanelCom3_2, 20);
    lv_obj_set_x( ui_PanelCom3_2, 16 );
    lv_obj_set_y( ui_PanelCom3_2, 0 );
    lv_obj_align( ui_PanelCom3_2, LV_ALIGN_RIGHT_MID, 5, 0);
    lv_obj_clear_flag( ui_PanelCom3_2, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelCom3_2, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelCom3_2, lv_color_hex(0xC6F531), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCom3_2, 255, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCom4 = lv_obj_create(ui_PanelFlag);
    lv_obj_set_width( ui_PanelCom4, 60);
    lv_obj_set_height( ui_PanelCom4, 30);
    lv_obj_set_x( ui_PanelCom4, 160 );
    lv_obj_set_y( ui_PanelCom4, 0 );
    lv_obj_set_align( ui_PanelCom4, LV_ALIGN_CENTER );
    lv_obj_clear_flag( ui_PanelCom4, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_bg_color(ui_PanelCom4, lv_color_hex(0x3D3B48), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCom4, 50, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCom4_1 = lv_obj_create(ui_PanelCom4);
    lv_obj_set_width( ui_PanelCom4_1, 20);
    lv_obj_set_height( ui_PanelCom4_1, 20);
    lv_obj_align( ui_PanelCom4_1, LV_ALIGN_LEFT_MID, -5, 0);
    lv_obj_clear_flag( ui_PanelCom4_1, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelCom4_1, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelCom4_1, lv_color_hex(0xC6F531), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCom4_1, 50, LV_PART_MAIN| LV_STATE_DEFAULT);

    ui_PanelCom4_2 = lv_obj_create(ui_PanelCom4);
    lv_obj_set_width( ui_PanelCom4_2, 20);
    lv_obj_set_height( ui_PanelCom4_2, 20);
    lv_obj_set_x( ui_PanelCom4_2, 16 );
    lv_obj_set_y( ui_PanelCom4_2, 0 );
    lv_obj_align( ui_PanelCom4_2, LV_ALIGN_RIGHT_MID, 5, 0);
    lv_obj_clear_flag( ui_PanelCom4_2, LV_OBJ_FLAG_SCROLLABLE );    /// Flags
    lv_obj_set_style_radius(ui_PanelCom4_2, 100, LV_PART_MAIN| LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_PanelCom4_2, lv_color_hex(0xC6F531), LV_PART_MAIN | LV_STATE_DEFAULT );
    lv_obj_set_style_bg_opa(ui_PanelCom4_2, 50, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    lv_timer = lv_timer_create(meter_value_refresh, 300, NULL);
    
    sync_ui(&multimeter); //同步ui控件

    return ui_screen;
}

static void update_ui(multimeter_t *m)
{
    //更新控件
    lv_obj_set_style_opa(ui_LabelAuto, m->gear_auto?255:0, LV_PART_MAIN| LV_STATE_DEFAULT);
    if(m->channel==kBoardMetetCh_R || m->channel==kBoardMetetCh_D)
        lv_label_set_text(ui_LabelRModeFlag, r_flag[m->r_mode]);
    else 
        lv_label_set_text(ui_LabelRModeFlag, r_flag[0]);
    
    if(m->channel==kBoardMetetCh_A) {
        lv_obj_set_style_bg_opa(ui_PanelCom3, 50, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(ui_PanelCom3_1, 50, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(ui_PanelCom3_2, 50, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(ui_PanelCom4, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(ui_PanelCom4_1, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(ui_PanelCom4_2, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
    }
    else {
        lv_obj_set_style_bg_opa(ui_PanelCom3, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(ui_PanelCom3_1, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(ui_PanelCom3_2, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(ui_PanelCom4, 50, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(ui_PanelCom4_1, 50, LV_PART_MAIN| LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(ui_PanelCom4_2, 50, LV_PART_MAIN| LV_STATE_DEFAULT);
    }
    
    lv_label_set_text(ui_LabelUnit, m->unit_name[m->para[m->id].unit]);
    
}

static void sync_ui(multimeter_t *m)
{
    lv_obj_add_state(ui_BtnSelect[m->channel], LV_STATE_CHECKED);
    lv_obj_set_style_text_color(ui_LabelSelect[m->channel], lv_color_hex(0xffffff), LV_PART_MAIN | LV_STATE_DEFAULT); 
    update_ui(m);
}

static void btn_select_event_cb(lv_event_t * e)
{
    lv_obj_t *t = e->target;
    uint8_t i;
    
    for(i=0;i!=4;i++) {
        lv_obj_clear_state( ui_BtnSelect[i], LV_STATE_CHECKED );
        lv_obj_set_style_text_color(ui_LabelSelect[i], lv_color_hex(0x808080), LV_PART_MAIN | LV_STATE_DEFAULT );
    }
    
    lv_obj_add_state(t, LV_STATE_CHECKED);
    
    for(i=0;i!=4;i++) { //找到是选择了哪个
        if(lv_obj_has_state(ui_BtnSelect[i], LV_STATE_CHECKED)) {
            lv_obj_set_style_text_color(ui_LabelSelect[i], lv_color_hex(0xffffff), LV_PART_MAIN | LV_STATE_DEFAULT); 
            break;
        }
    }
    
    multimeter_select_channel(channel[i]);
    //update_ui(&multimeter);
}

static void btn_mode_event_cb(lv_event_t * e)
{
    multimeter_select_next_mode();
}

static void btn_gear_event_cb(lv_event_t * e)
{
    if(multimeter.gear_auto==1) {
        multimeter.gear = 0;
        multimeter.gear_auto = 0;
    }
    else {
        multimeter.gear++;
        if(multimeter.gear>multimeter.gear_max) {
            multimeter.gear = 0;
            multimeter.gear_auto = 1;
        }
    }
    multimeter_select_gear(multimeter.gear);
    //update_ui(&multimeter);    
}

static void meter_value_refresh(lv_timer_t *timer)
{
    char value_str[16];
    int_to_str(value_str, multimeter.value, multimeter.para[multimeter.id].point, 6, NULL);    
    lv_label_set_text(ui_LabelValue,value_str);
    if(multimeter.update) {
        multimeter.update = 0;
        update_ui(&multimeter);
    }
}